package com.malou.client.ventas.cambio;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter.DateFilterMessages;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class CambioPanel extends ContentPanel{
	private MalouConstants constants = GWT.create(MalouConstants.class);
	private ToolBar toolBar;
	private Button nuevoCambio;		
	public  CambioDialog dialog;
	Integer selectedSize;
	private BeanModel selectedModel;
	
	public CambioPanel(){
		Cambios.getPaginate();
     	Cambios.getListIngresoStock();
    	Cambios.getListSalidaStock();

		final PagingToolBar toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		toolBarPag.bind(Cambios.loader);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
        centerData.setMargins(new Margins(0));
        
        BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST,500);
        eastData.setMargins(new Margins(0,0,0,5));
        
        BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH,250);
        southData.setMargins(new Margins(5,0,0,0));
        
        RowNumberer ri = new RowNumberer();
	    
        List<ColumnConfig> columnsIS = new ArrayList<ColumnConfig>();
            columnsIS.add(ri);
	        columnsIS.add(new ColumnConfig("prenda",constants.prendaingreso(),210));  	
	        columnsIS.add(new ColumnConfig("cantidad",constants.cantidad(),80));  	
	        columnsIS.add(new ColumnConfig("monto",constants.montoefectivo(),100));  	
	       
        ColumnModel cmis = new ColumnModel(columnsIS);
        Grid<BeanModel> gridIngresoStock = new Grid<BeanModel>(Cambios.storeListIngresoStock, cmis);  
        
        RowNumberer rs = new RowNumberer();
        List<ColumnConfig> columnsSS = new ArrayList<ColumnConfig>();
            columnsSS.add(rs);
	        columnsSS.add(new ColumnConfig("prenda",constants.prenda(),210));  	
	        columnsSS.add(new ColumnConfig("cantidad",constants.cantidad(),80));  	
	        columnsSS.add(new ColumnConfig("monto",constants.montoefectivo(),100));  	
	       
        ColumnModel cmss = new ColumnModel(columnsSS);
        Grid<BeanModel> gridSalidaStock = new Grid<BeanModel>(Cambios.storeListSalidaStock, cmss);  
        
        RowNumberer rc = new RowNumberer();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();	
		final CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
		columns.add(rc);
		columns.add(sm.getColumn());
		columns.add(new ColumnConfig("montorecibir",constants.montorecibir(), 100));
		columns.add(new ColumnConfig("montodevolver",constants.montodevolver(), 100));
		columns.add(new ColumnConfig("fecha", constants.fecha(),150));
			
		ColumnModel cm = new ColumnModel(columns);
		
		Grid<BeanModel> gridCambios = new Grid<BeanModel>(Cambios.store, cm);  
        gridCambios.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  Cambios.loader.load(0, Utilities.itemsPerPage);
          }  
        });
        
        gridCambios.setSelectionModel(sm);
        gridCambios.getSelectionModel().addListener(Events.SelectionChange,
	        new Listener<SelectionChangedEvent<BeanModel>>() {
	
			@Override
			public void handleEvent(SelectionChangedEvent<BeanModel> be) {
				selectedSize = be.getSelection().size();
				if(selectedSize==0){						
					Cambios.storeListIngresoStock.removeAll();
					Cambios.storeListSalidaStock.removeAll();
				}else if(selectedSize==1){
					selectedModel = be.getSelectedItem();						
					Cambios.cambio_id = selectedModel.get("id");
					System.out.println("el cambio_id ="+selectedModel.get("id"));
					Cambios.loaderListIngresoStock.load();
					Cambios.loaderListSalidaStock.load();
				}else if(selectedSize > 1){
				    Cambios.storeListIngresoStock.removeAll();
					Cambios.storeListSalidaStock.removeAll();
				}
			}
	});

        gridCambios.setLoadMask(true);  
        gridCambios.setBorders(true);
                
        setFrame(true);  
        setCollapsible(true);  
        setAnimCollapse(false);  
        
        setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.gastoLista());  
        setLayout(new BorderLayout());
        
        toolBar = new ToolBar();
        
        nuevoCambio = new Button(constants.nuevoCambio(), IconHelper.createPath("images/icons/addstock.png"));
        
        dialog = new CambioDialog();
        SelectionListener<ButtonEvent> sl_new;       
        sl_new = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {	
				dialog.clearPrincipal();				
			    dialog.show();			    
			}
        };
        nuevoCambio.addSelectionListener(sl_new);
        toolBar.add(nuevoCambio);             
        
        setTopComponent(toolBar);             
        
        GridFilters filters = new GridFilters();
        filters.getMessages().setFilterText(constants.filtros());
        
        StringFilter prendaIngresoFilter = new StringFilter("prendaingreso");
        StringFilter prendaSalidaFilter = new StringFilter("prendasalida");
        DateFilter fechaFilter = new DateFilter("fecha");
        
        DateFilterMessages dm= new DateFilterMessages();
        dm.setAfterText(constants.despues());
        dm.setBeforeText(constants.antes());
        dm.setOnText(constants.del());
        fechaFilter.setMessages(dm);
        
        filters.addFilter(prendaIngresoFilter);        
        filters.addFilter(prendaSalidaFilter);        
                
        gridCambios.addPlugin(filters);
    
        LayoutContainer detalleIngresoStock = new LayoutContainer();
        detalleIngresoStock.setLayout(new BorderLayout());
    
        LayoutContainer detalleSalidaStock = new LayoutContainer();
        detalleSalidaStock.setLayout(new BorderLayout());
        
        LayoutContainer detalleSalidaIngreso = new LayoutContainer();
        detalleSalidaIngreso.setLayout(new BorderLayout());
        
        detalleSalidaIngreso.add(gridIngresoStock,centerData);
        detalleSalidaIngreso.add(gridSalidaStock,southData);
        
        ContentPanel maestro = new ContentPanel();
        	maestro.setHeaderVisible(false);
        	maestro.setLayout(new FitLayout());
        	maestro.add(gridCambios);
        	maestro.setBottomComponent(toolBarPag);
        add(maestro, centerData);
        
        add(detalleSalidaIngreso,eastData);
	}
	
	public void setPermisos(List<String> permisos){
		
		boolean add = permisos.contains("realizar");		
		
		if(!add){
			toolBar.setVisible(false);
		}		
	}
}
